<template>
  <div style="padding: 20px 20px;">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="代码" prop="basicCode">
        <el-input
          key="queryBasicCode"
          v-model="queryParams.basicCode"
          placeholder="请输入代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="名称" prop="basicName">
        <el-input
          key="queryBasicName"
          v-model="queryParams.basicName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="属性分类" prop="attributeType">
        <el-select v-model="queryParams.attributeType" clearable placeholder="请选择">
          <el-option
            v-for="dict in dict.type.field_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          key="refField"
          type="primary"
          plain
          icon="el-icon-money"
          size="mini"
          @click="handleRef"
        >引用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          key="delRefList"
          type="primary"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除引用</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="fieldList" @selection-change="handleSelectionChange" style="border: 1px solid #e6ebf5;" border>
      <el-table-column key="fselection" type="selection" width="55" align="center" />
      <el-table-column key="fbasicName" label="名称" align="center" prop="basicName" sortable/>
      <el-table-column key="fbasicJavaType" label="数据类型" align="center" prop="basicJavaType" sortable>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_java_type" :value="scope.row.basicJavaType"/>
        </template>
      </el-table-column>
      <el-table-column key="fformTypeName" label="页面元素" align="center" prop="formTypeName" sortable>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.form_type_name" :value="scope.row.formTypeName"/>
        </template>
      </el-table-column>
<!--      <el-table-column key="fsortNum" label="排序" align="center" prop="sortNum" sortable/>-->
      <el-table-column key="foper" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            key="delRef"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除引用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-if="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <select-field ref="selectField" @getList="getList"/>

  </div>

</template>
<style lang="scss" scoped>
::v-deep .el-tabs--card > .el-tabs__header .el-tabs__item.is-active {
  background-color: #409EFF;
  color: #FFFFFF;
}
::v-deep .el-tabs__item {
  cursor: pointer;
  padding: 0 14px;
  height: 26px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  line-height: 26px;
  display: inline-block;
  list-style: none;
  font-size: 12px;
  /*font-weight: 500;*/
  color: #303133;
  position: relative;
}
div#pane-second {
  position: relative;
  height: 76vh;
  background-color: #f9f9f9
}
.el-card.box-card.is-always-shadow {
/*  width: 96%;
  left: 2%;*/
  position: relative;
}
#pane-first{
  margin-top:15px ;
}
#pane-third .el-icon-caret-right {
  display: none;
}
</style>
<script>
import {addField, updateField, listRefField, delRefField} from "@/api/form/field";
import selectField from "./selectfield";

export default {
  components: { selectField},
  dicts: ['basic_java_type', 'form_type_name','form_type_name', 'data_extend', 'logic_yes_no', 'field_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      attributeOpen: false,
      // 总条数
      total: 0,
      // 表格数据
      fieldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dictList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelFlag: null,
        modelId: null,
        basicCode: null,
        basicName: null,
        dataExtend: null,
        attributeType: null
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.queryParams.modelId = localStorage.getItem("ls_model_id");
    this.getList();
  },
  methods: {
    /** 查询属性列表 */
    getList() {
      this.loading = true;
      listRefField(this.queryParams).then(response => {
        this.fieldList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 选择引用属性 */
    handleRef() {
      let modelId = this.queryParams.modelId;
      if (modelId == null && this.queryParams.parentId != null) {
        modelId = this.queryParams.parentId;
      }
      this.$refs.selectField.showDialog(modelId);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateField(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(error=>{
              console.log(error);
              this.loading = false;
            });
          } else {
            addField(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.loadTree();
            }).catch(error=>{
              console.log(error);
              this.loading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let ids = row.id || this.ids;
      let modelId = this.queryParams.modelId;
      this.$modal.confirm('是否确认删除数据项？').then(function() {
        if (row.id) {
          ids = row.id.split(",");
        }
        return delRefField({refIds: ids, modelId: modelId});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
        this.loadTree();
      }).catch(() => {});
    },
  }
};
</script>
